package com.gztms.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * @author Zoey
 * @date 2023/4/20 20:53
 * @Description 文件上传工具
 **/
public class UploadUtil {
    // 阿里云oss域名
    public static final String ALI_DOMAIN = "https://gztour-attraction.oss-cn-guangzhou.aliyuncs.com/";

    public static String uploadImage(MultipartFile file) throws IOException {
        // 生成文件名
        String originalFilename = file.getOriginalFilename(); // 获取原来的文件名
        String ext = "." + FilenameUtils.getExtension(originalFilename); // 获取文件拓展名
        String uuid = UUID.randomUUID().toString().replace("-","");
        String filename = uuid + ext;

        // 地域节点
        String endpoint = "http://oss-cn-guangzhou.aliyuncs.com";
        String accessKeyId = "LTAI5tJkp8oVd1DYeSYdhKtK";
        String accessKeySecret = "xjGbke0MUjaqtrIuCFo7FeSRs11zof";

        // OSS客户端对象
        OSS ossClient = new OSSClientBuilder().build(endpoint,accessKeyId,accessKeySecret);
        ossClient.putObject("gztour-attraction", filename, file.getInputStream());
        // 关闭
        ossClient.shutdown();
        // 返回图片路径
        return ALI_DOMAIN + filename;

    }


}
